// <editor-fold defaultstate="collapsed" desc="GNU GPLv3 Header">
/*
 * File:   GenericFormatChunkWaveFile.java
 *
 * Copyright (C) 2013 Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * Copyright (C) 2013 David Megias Jimenez <dmegias@uoc.edu>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
// </editor-fold>
package readwritewave;

// <editor-fold defaultstate="collapsed" desc="imports">
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
// </editor-fold>

/**
 * class for storing the generic format chunk of a wave audio file.
 *
 * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * @version 1.0
 * @since 2013-08-04
 */
class GenericFormatChunkWaveFile extends GenericChunkWaveFile {

    /**
     * Microsoft Pulse Code Modulation (PCM) format.
     */
    public static final int WAVE_FORMAT_PCM = 0x0001;
    /**
     * IBM mu-law format
     */
    public static final int IBM_FORMAT_MULAW = 0x0101;
    /**
     * IBM a-law format
     */
    public static final int IBM_FORMAT_ALAW = 0x0102;
    /**
     * IBM AVC Adaptive Differential Pulse Code Modulation format
     */
    public static final int IBM_FORMAT_ADPCM = 0x0103;
    /**
     * A number indicating the WAVE format category of the file.
     */
    short wFormatTag;
    /**
     * The number of channels represented in the waveform data, such as 1 for
     * mono or 2 for stereo.
     */
    short wChannels;
    /**
     * The sampling rate (in samples per second) at which each channel should
     * be played.
     */
    int dwSamplesPerSec;
    /**
     * The average number of bytes per second at which the waveform data should
     * be transferred. Playback software can estimate the buffer size using this
     * value.
     */
    int dwAvgBytesPerSec;
    /**
     * The block alignment (in bytes) of the waveform data. Playback software
     * needs to process a multiple of wBlockAlign bytes of data at a time, so
     * the value of wBlockAlign can be used for buffer alignment.
     */
    short wBlockAlign;

    /**
     *
     * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
     * @version 1.0
     * @since 2013-08-04
     */
    GenericFormatChunkWaveFile() {
    }

    /**
     *
     * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
     * @version 1.0
     * @since 2013-08-04
     */
    GenericFormatChunkWaveFile(long lngStartParam, String strIDParam,
            int lngSizeParam, short wFormatTagParam, short wChannelsParam,
            int dwSamplesPerSecParam, int dwAvgBytesPerSecParam,
            short wBlockAlignParam) throws IOException {
        super(lngStartParam, strIDParam, lngSizeParam);
        wFormatTag = wFormatTagParam;
        wChannels = wChannelsParam;
        dwSamplesPerSec = dwSamplesPerSecParam;
        dwAvgBytesPerSec = dwAvgBytesPerSecParam;
        wBlockAlign = wBlockAlignParam;
    }

    /**
     *
     * @param dis
     *
     * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
     * @version 1.0
     * @since 2013-08-04
     */
    GenericFormatChunkWaveFile(DataInputStream dis) throws IOException {
        super(12);
        readID(dis);
        if (!idIsFMT()) {
            throw new UnsupportedOperationException("Not yet implemented, only "
                    + "supports 'fmt '.");
        }
        readSize(dis);
        readWFormatTag(dis);
        readWChannels(dis);
        readDWSamplesPerSec(dis);
        readDWAvgBytesPerSec(dis);
        readWBlockAlign(dis);
    }

    /**
     *
     * @param dis
     *
     * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
     * @version 1.0
     * @since 2013-08-04
     */
    private void readWFormatTag(DataInputStream dis) throws IOException {
        wFormatTag = readShortLE(dis);
    }

    /**
     *
     * @return
     *
     * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
     * @version 1.0
     * @since 2013-08-04
     */
    public short getWFormatTag() {
        return wFormatTag;
    }

    /**
     *
     * @param dis
     *
     * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
     * @version 1.0
     * @since 2013-08-04
     */
    private void readWChannels(DataInputStream dis) throws IOException {
        wChannels = readShortLE(dis);
    }

    /**
     *
     * @param dis
     *
     * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
     * @version 1.0
     * @since 2013-08-04
     */
    private void readDWSamplesPerSec(DataInputStream dis) throws IOException {
        dwSamplesPerSec = readLongLE(dis);
    }

    /**
     *
     * @param dis
     *
     * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
     * @version 1.0
     * @since 2013-08-04
     */
    private void readDWAvgBytesPerSec(DataInputStream dis) throws IOException {
        dwAvgBytesPerSec = readLongLE(dis);
    }

    /**
     *
     * @param dis
     *
     * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
     * @version 1.0
     * @since 2013-08-04
     */
    private void readWBlockAlign(DataInputStream dis) throws IOException {
        wBlockAlign = readShortLE(dis);
    }

    /**
     *
     * @param dos
     *
     * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
     * @version 1.0
     * @since 2013-08-04
     */
    public void writeWFormatTag(DataOutputStream dos) throws IOException {
        writeShortLE(wFormatTag, dos);
    }

    /**
     *
     * @param dos
     *
     * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
     * @version 1.0
     * @since 2013-08-04
     */
    public void writeWChannels(DataOutputStream dos) throws IOException {
        writeShortLE(wChannels, dos);
    }

    /**
     *
     * @param dos
     *
     * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
     * @version 1.0
     * @since 2013-08-04
     */
    public void writeDWSamplesPerSec(DataOutputStream dos) throws IOException {
        writeLongLE(dwSamplesPerSec, dos);
    }

    /**
     *
     * @param dos
     *
     * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
     * @version 1.0
     * @since 2013-08-04
     */
    public void writeDWAvgBytesPerSec(DataOutputStream dos) throws IOException {
        writeLongLE(dwAvgBytesPerSec, dos);
    }

    /**
     *
     * @param dos
     * 
     * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
     * @version 1.0
     * @since 2013-08-04
     */
    public void writeWBlockAlign(DataOutputStream dos) throws IOException {
        writeShortLE(wBlockAlign, dos);
    }

    /**
     *
     * @return true if wFormatTag == WAVE_FORMAT_PCM
     *
     * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
     * @version 1.0
     * @since 2013-08-05
     */
    public boolean formatIsPCM() {
        return (wFormatTag == WAVE_FORMAT_PCM);
    }

    short getWChannels() {
        return wChannels;
    }

    /**
     *
     * @return
     *
     * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
     * @version 1.0
     * @since 2013-08-05
     */
    private boolean idIsFMT() {
        return (strID.equals("fmt "));
    }
}